package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.FormatTool;
import com.j283_5.wisdom_park.mapper.*;
import com.j283_5.wisdom_park.pojo.*;
import com.j283_5.wisdom_park.service.PropertyOverviewService;
import com.j283_5.wisdom_park.vo.property_overview.ChartVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.util.*;

@Service
public class PropertyOverviewServiceImpl implements PropertyOverviewService {
    @Resource
    private DeviceInformationMapper deviceInformationMapper;
    @Resource
    private TenantbillsinfoMapper tenantbillsinfoMapper;
    @Resource
    private CarBillsMapper carBillsMapper;
    @Resource
    private VisitRecordMapper visitRecordMapper;
    @Resource
    private CarRecordMapper carRecordMapper;
    @Resource
    private WarningRecordMapper warningRecordMapper;
    @Resource
    private PropertyPayCostMapper propertyPayCostMapper;

    @Override
    public ResultData numberOfEquipmentInNormal() {
        QueryWrapper<DeviceInformation> deviceInformationQueryWrapper = new QueryWrapper<>();
        deviceInformationQueryWrapper.eq("dinf_state",0);
        Integer equipmentInNormalCount = deviceInformationMapper.selectCount(deviceInformationQueryWrapper);
        if(equipmentInNormalCount != null){
            return ResultData.ok().data(equipmentInNormalCount);
        }else{
            return ResultData.error().message("查询失败！");
        }
    }

    @Override
    public ResultData annualAccumulativePayment() {
        QueryWrapper<Tenantbillsinfo> tenantbillsinfoQueryWrapper = new QueryWrapper<>();
        tenantbillsinfoQueryWrapper.select("IFNULL(sum(ten_money),0) as tenMoney");
        tenantbillsinfoQueryWrapper.eq("ten_isPay",1);
        Tenantbillsinfo tenantbillsinfo = tenantbillsinfoMapper.selectOne(tenantbillsinfoQueryWrapper);
        Double tenMoneys = tenantbillsinfo.getTenMoney();
        QueryWrapper<CarBills> carBillsQueryWrapper = new QueryWrapper<>();
        carBillsQueryWrapper.select("IFNULL(SUM(car_money),0) as carMoney").eq("car_ispay",1);
        CarBills carBills =
                carBillsMapper.selectOne(carBillsQueryWrapper);
        Double carMoneys = carBills.getCarMoney();
        if(tenMoneys != null ){
            return ResultData.ok().data(tenMoneys+carMoneys);
        }else{
            return ResultData.error().message("查询失败");
        }
    }

    @Override
    public ResultData numberOfPeopleEntering() {
        QueryWrapper<VisitRecord> visitRecordQueryWrapper = new QueryWrapper<>();
        Integer peopleEnteringCount = visitRecordMapper.selectCount(visitRecordQueryWrapper);
        if(peopleEnteringCount != null){
            return ResultData.ok().data(peopleEnteringCount);
        }else{
            return ResultData.error().message("查询失败");
        }
    }

    @Override
    public ResultData incomingAndOutgoingToday() {
        QueryWrapper<CarRecord> carRecordQueryWrapper = new QueryWrapper<>();
        Integer outgoingTodayCount = carRecordMapper.selectCount(carRecordQueryWrapper);
        if(outgoingTodayCount != null){
            return ResultData.ok().data(outgoingTodayCount);
        }else{
            return ResultData.error().message("查询失败");
        }
    }

    @Override
    public ResultData deviceErrorCount(Integer way) {
        ArrayList<ChartVo> chartVoList = new ArrayList<>();
        String thisYear = getThisYear();
        if(way == 0){
            for(int i=0 ; i<10 ; i++){
                String minYear = (Integer.parseInt(thisYear) - i)+"";
                Date minDate = FormatTool.getDate(minYear, "yyyy");
                String maxYear = (Integer.parseInt(thisYear) - i +1)+"";
                Date maxDate = FormatTool.getDate(maxYear, "yyyy");
                QueryWrapper<WarningRecord> warningRecordQueryWrapper = new QueryWrapper<>();
                warningRecordQueryWrapper.gt("wrec_date",minDate);
                warningRecordQueryWrapper.lt("wrec_date",maxDate);
                Integer count = warningRecordMapper.selectCount(warningRecordQueryWrapper);
                chartVoList.add(new ChartVo((Integer.parseInt(minYear))+"年",count));
            }
        }else if(way == 1){
            ChartVo spring = new ChartVo("spring", 0);
            ChartVo summer = new ChartVo("summer", 0);
            ChartVo autumn = new ChartVo("autumn", 0);
            ChartVo winter = new ChartVo("winter", 0);
            for(int i=0 ; i<13 ; i++){
                Integer count = getCount(thisYear, i);
                setSeason(spring, summer, autumn, winter, i, count);
            }
            chartVoList.add(spring);
            chartVoList.add(summer);
            chartVoList.add(autumn);
            chartVoList.add(winter);
        }else if(way == 2){
            for(int i=1 ; i<13 ; i++){
                Integer count = getCount(thisYear, i);
                ChartVo chartVo = new ChartVo(i + "月", count);
                chartVoList.add(chartVo);
            }
        }
        if(chartVoList.size()>0){
            return ResultData.ok().data(chartVoList);
        }else{
            return ResultData.error().message("查询失败!");
        }
    }

    @Override
    public ResultData propertyPayCost(Integer way,Integer type) {
        ArrayList<ChartVo> chartVoList = new ArrayList<>();
        String thisYear = getThisYear();
        if(way == 0){
            for(int i=0 ; i<10 ; i++){
                String minYear = (Integer.parseInt(thisYear) - i)+"";
                Date minDate = FormatTool.getDate(minYear, "yyyy");
                String maxYear = (Integer.parseInt(thisYear) - i +1)+"";
                Date maxDate = FormatTool.getDate(maxYear, "yyyy");
                QueryWrapper<PropertyPayCost> warningRecordQueryWrapper = new QueryWrapper<>();
                warningRecordQueryWrapper.select("IFNULL(SUM(ppc_money),0) as ppcMoney");
                warningRecordQueryWrapper.eq("ppc_type",type);
                warningRecordQueryWrapper.gt("ppc_real_date",minDate);
                warningRecordQueryWrapper.lt("ppc_real_date",maxDate);
                PropertyPayCost propertyPayCost = propertyPayCostMapper.selectOne(warningRecordQueryWrapper);
                Double count = propertyPayCost.getPpcMoney();
                if(count != null) {
                    chartVoList.add(new ChartVo((Integer.parseInt(minYear)) + "年", count));
                }else{
                    chartVoList.add(new ChartVo((Integer.parseInt(minYear)) + "年", 0));
                }
            }
        }else if(way == 1){
            ChartVo spring = new ChartVo("spring", 0);
            ChartVo summer = new ChartVo("summer", 0);
            ChartVo autumn = new ChartVo("autumn", 0);
            ChartVo winter = new ChartVo("winter", 0);
            for(int i=0 ; i<13 ; i++){
                double count = getProPerCostCount(thisYear, i,type);
                setSeason(spring, summer, autumn, winter, i, count);
            }
            chartVoList.add(spring);
            chartVoList.add(summer);
            chartVoList.add(autumn);
            chartVoList.add(winter);
        }else if(way == 2){
            for(int i=1 ; i<13 ; i++){
                double count = getProPerCostCount(thisYear, i,type);
                ChartVo chartVo = new ChartVo(i + "月", count);
                chartVoList.add(chartVo);
            }
        }



        if(chartVoList.size()>0){
            return ResultData.ok().data(chartVoList);
        }else{
            return ResultData.error().message("查询失败!");
        }
    }

    @Override
    public ResultData carPayment(Integer way) {

        ArrayList<ChartVo> chartVoList = new ArrayList<>();
        String thisYear = getThisYear();
        if(way == 0){
            for(int i=0 ; i<10 ; i++){
                String minYear = (Integer.parseInt(thisYear) - i)+"";
                Date minDate = FormatTool.getDate(minYear, "yyyy");
                String maxYear = (Integer.parseInt(thisYear) - i +1)+"";
                Date maxDate = FormatTool.getDate(maxYear, "yyyy");

                QueryWrapper<CarBills> carBillsQueryWrapper = new QueryWrapper<>();
                carBillsQueryWrapper.select("IFNULL(SUM(car_money),0) as carMoney");
                carBillsQueryWrapper.eq("car_isPay",1);
                carBillsQueryWrapper.gt("car_bilesTime",minDate);
                carBillsQueryWrapper.lt("car_bilesTime",maxDate);
                CarBills carBills = carBillsMapper.selectOne(carBillsQueryWrapper);
                Double count = carBills.getCarMoney();
                if(count != null) {
                    chartVoList.add(new ChartVo((Integer.parseInt(minYear)) + "年", count));
                }else{
                    chartVoList.add(new ChartVo((Integer.parseInt(minYear)) + "年", 0));
                }
            }
        }else if(way == 1){
            ChartVo spring = new ChartVo("spring", 0);
            ChartVo summer = new ChartVo("summer", 0);
            ChartVo autumn = new ChartVo("autumn", 0);
            ChartVo winter = new ChartVo("winter", 0);
            for(int i=0 ; i<13 ; i++){
                double count = getCarCount(thisYear, i);
                setSeason(spring, summer, autumn, winter, i, count);
            }
            chartVoList.add(spring);
            chartVoList.add(summer);
            chartVoList.add(autumn);
            chartVoList.add(winter);
        }else if(way == 2){
            for(int i=1 ; i<13 ; i++){
                double count = getCarCount(thisYear, i);
                ChartVo chartVo = new ChartVo(i + "月", count);
                chartVoList.add(chartVo);
            }
        }



        if(chartVoList.size()>0){
            return ResultData.ok().data(chartVoList);
        }else{
            return ResultData.error().message("查询失败!");
        }
    }


    private void setSeason(ChartVo spring, ChartVo summer, ChartVo autumn, ChartVo winter, int i, double count) {
        switch (i){
            case 3:
            case 4:
            case 5:
                spring.setNum(spring.getNum()+count);
                break;
            case 6:
            case 7:
            case 8:
                summer.setNum(summer.getNum()+count);
                break;
            case 9:
            case 10:
            case 11:
                autumn.setNum(autumn.getNum()+count);
            case 12:
            case 1:
            case 2:
                winter.setNum(winter.getNum()+count);
        }
    }

    private Double getProPerCostCount(String thisYear, int i,int type) {
        Map<String, Date> dateMap = getDate(thisYear,i);
        QueryWrapper<PropertyPayCost> warningRecordQueryWrapper = new QueryWrapper<>();
        warningRecordQueryWrapper.select("IFNULL(SUM(ppc_money),0) as ppcMoney");
        warningRecordQueryWrapper.eq("ppc_type",type);
        warningRecordQueryWrapper.gt("ppc_real_date",dateMap.get("minDate"));
        warningRecordQueryWrapper.lt("ppc_real_date",dateMap.get("maxDate"));
        PropertyPayCost propertyPayCost = propertyPayCostMapper.selectOne(warningRecordQueryWrapper);
        Double count = propertyPayCost.getPpcMoney();

        if(count != null){
            return count;
        }else{
            return 0.0;
        }
    }

    private Double getCarCount(String thisYear, int i) {
        Map<String, Date> dateMap = getDate(thisYear,i);
        QueryWrapper<CarBills> carBillsQueryWrapper = new QueryWrapper<>();
        carBillsQueryWrapper.select("IFNULL(SUM(car_money),0) as carMoney");
        carBillsQueryWrapper.eq("car_isPay",1);
        carBillsQueryWrapper.gt("car_bilesTime",dateMap.get("minDate"));
        carBillsQueryWrapper.lt("car_bilesTime",dateMap.get("maxDate"));
        CarBills carBills = carBillsMapper.selectOne(carBillsQueryWrapper);
        Double count = carBills.getCarMoney();
        if(count != null){
            return count;
        }else{
            return 0.0;
        }
    }

    private Integer getCount(String thisYear, int i) {
        Map<String, Date> dateMap = getDate(thisYear,i);
        QueryWrapper<WarningRecord> warningRecordQueryWrapper = new QueryWrapper<>();
        warningRecordQueryWrapper.gt("wrec_date",dateMap.get("minDate"));
        warningRecordQueryWrapper.lt("wrec_date",dateMap.get("maxDate"));
        Integer count = warningRecordMapper.selectCount(warningRecordQueryWrapper);
        return count;
    }

    /**
     * 获取年份
     * @return
     */
    private String getThisYear(){
        Date date = new Date(System.currentTimeMillis());
        return  FormatTool.getStringDate(date, "yyyy");
    }
    /**
     * 返回最大日期和最小日期
     */
    private Map<String,Date> getDate(String year , Integer month){
        String yearMonth;
        if(month <10) {
            yearMonth =  year + "-0" + month;
        }else{
            yearMonth = year + "-" + month;
        }
        Date minDate = FormatTool.getDate(yearMonth, "yyyy-MM");
        Integer day = getDay(Integer.parseInt(year), month+1);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(minDate);
        calendar.add(calendar.DATE,day);
        Date maxDate = calendar.getTime();
        HashMap<String, Date> map = new HashMap<>();

        map.put("minDate",minDate);
        map.put("maxDate",maxDate);
        return map;
    }

    /**
     * 获取这个月的天数
     */
    private Integer getDay(Integer year ,Integer month){
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 2:
                if(year%400==0 || (year%400!=0 && year%4==0)){
                    return 29;
                }else{
                    return 28;
                }
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            default:return 0;
        }
    }
}
